/**
 * 作者: solin
 * 时间: 2017年10月24日 下午6:38:33
 */
var Windex = parent.layer.getFrameIndex(window.name);
$(function(){
	$('input[type="radio"]').iCheck({radioClass: 'iradio_square-green'});
	
	// 客户等级下拉框
	sys.selectpicker({
		id : 'customerLevel',
		url : rootPath + '/customerLevel/listCustomerLevel',
		valueField : 'id',
		textField : 'levelName',
		defaultValue : null,
	})
	
	// 销售人员
	$.ajax({
		type:'POST',
		url: rootPath + '/dict/listKeyMapEntites',
		data : { keyType : "salesmanRoleId" },
		dataType: 'json',
		success: function(data) {  
			if (data.success == '1' && data.data.length > 0) {
				sys.selectpicker({
					id : 'salesmanCode',
					url : rootPath + '/user/getSalesmanList',
					param: {roleId: data.data[0].keyValue},
					valueField : 'id',
					textField : 'userName',
					defaultValue : null,
				})
			}
		}
	})
	
	$('#btnSave').on('click', function() {
		var status = $('input:radio[name="status"]:checked').val();
		if('1' == status) {
			var customerLevel = $("#customerLevel").val();
			if(!customerLevel) {
				showErr('请选择客户等级');
				return;
			}
			var salesmanCode = $("#salesmanCode").val();
			if(!salesmanCode) {
				showErr('请选择销售人员！');
				return;
			}
		}
		
		$.ajax({
			type: 'POST',
			url: rootPath + "/customer/saveOrUpdateCustomer",
			async: false,
			data: {
				optType: 'audit',
				id: $('#customerId').val(), 
				status: status,
				customerLevel: $("#customerLevel").val(),
				salesmanCode: $("#salesmanCode").val(),
				auditRemark: $('#auditRemark').val()
			},
			dataType: 'json',
			success: function(data) {
				if (data.code == '1') {
					parent.auditStatus = status;
					parent.$('#layer_opt_status').val('1'); 
					parent.layer.close(Windex);
				}
			}
		});
	})
	
	//点击返回事件
	$("#btnCancel").on('click', function() {
   		parent.$('#layer_opt_status').val('0'); 
   		parent.layer.close(Windex);
	});
})